package com.lims.action.base;

import java.text.SimpleDateFormat;
import java.util.Date;

import com.lims.service.admin.IAdminService;
import com.lims.service.common.IDepartmentService;
import com.lims.service.common.IEnPartyService;
import com.lims.service.common.IProjectService;
import com.lims.service.common.ISampleService;
import com.lims.service.common.ISampleTypeService;
import com.lims.service.common.ITaskService;
import com.lims.service.common.ITestItemService;
import com.lims.service.common.ITestOfProjectService;
import com.lims.util.Pagination;
import com.lims.util.Toolkit;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;







public class BaseAction extends ActionSupport {

	private static final long serialVersionUID = -5711504813205222975L;
	
	
	protected Pagination pagination;
	protected String page;
	protected String size;
	protected IAdminService adminService;
	protected IProjectService projectService;
	protected IEnPartyService enPartyService;
	protected ISampleService sampleService;
	protected ISampleTypeService sampleTypeService;
	protected ITestItemService testItemService;
	protected ITestOfProjectService testOfProjectService;
	protected IDepartmentService departmentService;
	protected ITaskService  taskService;
	/*
	 * BaseAction 为一个单体类，即它只有一个实例。 -- Singleton
	 */
	private static BaseAction instance = null;

	public static BaseAction getInstance() {

		if (instance == null)
			instance = new BaseAction();
		return instance;
	}

	/*
	 * 定义表现层中要使用的常量。
	 */ 
	
	
	public static String WEBROOT_BASE_PATH = null; 	// 项目根目录
	
	public static Integer DEFAULT_PAGE_SIZE = 0; 	// 项目分页查询默认页面大小

	public static final String CUSTOMER_IN_SESSION = "srp.session.customer";	// session中保存当前Customer的字段名称

	static {
		WEBROOT_BASE_PATH = Toolkit.readProperty("webroot");
		DEFAULT_PAGE_SIZE = Integer.parseInt(Toolkit.readProperty("pagesize"));
	}

	
	
	
//	/*
//	 * 声明使用到的Service实例，这些实例由Spring依赖注入。
//	 */
	
	
	
	
	
	
	
	
	
	
	
	
	
	/**
	 * 根据页面的设置分页参数:每页多少条记录,当前第几页
	 * @author 冯晶晶
	 *@date 2009-10-26
	 * @param count 总记录数
	 */
	public void setPaginationParameter(long count) {

		if (pagination == null) {
			pagination = new Pagination((int) count, 20);
			pagination.setCurrentPage(1);
		}
		int size = pagination.getPageSize();
		try {
			if (getParameterValue("size") != null) {
				size = Integer.parseInt(getParameterValue("size"));
			}

		} catch (NumberFormatException e) {
//			e.printStackTrace();
			size = pagination.getPageSize();
		}
		if (size != 0) {
			pagination.setPageSize(size);
		}
		int page = 1;
		try {
			if (getParameterValue("page") != null) {
				page = Integer.parseInt(getParameterValue("page"));
			}
		} catch (NumberFormatException e) {
//			e.printStackTrace();
			page = 1;
		}
		pagination.setCurrentPage(page);
	}

	public Pagination getPagination() {
		return pagination;
	}

	public void setPagination(Pagination pagination) {
		this.pagination = pagination;
	}
	// 从Request中取值。
	protected String getParameterValue(String key) {
		Object value = ActionContext.getContext().getParameters().get(key);
		if (value instanceof String[]) {
			return ((String[]) value)[0];
		} else {
			if (value != null)
				return value.toString();
			return null;
		}
	}
	protected String[] getParameterValues(String key) {
		Object value = ActionContext.getContext().getParameters().get(key);
		if (value instanceof String[]) {
			return ((String[]) value);
		} else {
			return null;
		}
	}
	public String getPage() {
		return page;
	}

	public void setPage(String page) {
		this.page = page;
	}

	public String getSize() {
		return size;
	}

	public void setSize(String size) {
		this.size = size;
	}



	protected String getNowDateTime() {
		SimpleDateFormat df=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		return df.format(new Date());
	}

	public IAdminService getAdminService() {
		return adminService;
	}

	public void setAdminService(IAdminService adminService) {
		this.adminService = adminService;
	}

	public IProjectService getProjectService() {
		return projectService;
	}

	public void setProjectService(IProjectService projectService) {
		this.projectService = projectService;
	}

	public IEnPartyService getEnPartyService() {
		return enPartyService;
	}

	public void setEnPartyService(IEnPartyService enPartyService) {
		this.enPartyService = enPartyService;
	}

	public ISampleService getSampleService() {
		return sampleService;
	}

	public void setSampleService(ISampleService sampleService) {
		this.sampleService = sampleService;
	}

	public ISampleTypeService getSampleTypeService() {
		return sampleTypeService;
	}

	public void setSampleTypeService(ISampleTypeService sampleTypeService) {
		this.sampleTypeService = sampleTypeService;
	}

	public ITestItemService getTestItemService() {
		return testItemService;
	}

	public void setTestItemService(ITestItemService testItemService) {
		this.testItemService = testItemService;
	}

	public ITestOfProjectService getTestOfProjectService() {
		return testOfProjectService;
	}

	public void setTestOfProjectService(ITestOfProjectService testOfProjectService) {
		this.testOfProjectService = testOfProjectService;
	}

	public IDepartmentService getDepartmentService() {
		return departmentService;
	}

	public void setDepartmentService(IDepartmentService departmentService) {
		this.departmentService = departmentService;
	}

	public ITaskService getTaskService() {
		return taskService;
	}

	public void setTaskService(ITaskService taskService) {
		this.taskService = taskService;
	}


}
